<?php
/**[后台登陆控制器]
 * @Author: chen
 * @Date:   2014-08-17 17:43:18
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-11-08 09:19:09
 */
class LoginAction extends ExtendAction{


	/**
	 * [index 登录]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 验证是否登录
		if(isset($_SESSION['uid']) && $_SESSION['username'])
		{
			$this->success('您已经登录',U('Index/index'));
			die;
		}
		if(IS_POST)
		{
			
		}
		$this->display();
	}


	/**
	 * [ajax_check_login ajax验证登录]
	 * @return [type] [description]
	 */
	public function ajax_check_login()
	{
		if(!IS_AJAX)  $this->error('链接错误');
		$result = array('error'=>0,'msg'=>'');
		$userModel = D('User');
		// 验证码
		if(!$userModel->check_code()) 
		{
			$result['error']=1;
			$result['msg'] = $userModel->getError();
			$this->ajaxReturn($result);
		}
		// 验证用户名和密码
		if(!$user=$userModel->validate_login())
		{

			$result['error']=1;
			$result['msg'] = $userModel->getError();
			$this->ajaxReturn($result);
		}
		// 更新信息
		$userModel->update_login();
		// 保存登录信息
		$auto = I('post.auto');
		if($auto)
		{
			setcookie(session_name(),session_id(),time()+3600*24*7,'/');
		}
		else
		{
			setcookie(session_name(),session_id(),0,'/');
		}
		// 写入session
		session('uid',$user['uid']);
		session('nickname',$user['nickname']);
		session('username',$user['username']);
		session('roleinfo',$user['role']);
		$this->ajaxReturn($result);
	}


	/**
	 * [out 退出]
	 * @return [type] [description]
	 */
	public function out()
	{
		/*session('uid',null);
		session('nickname',null);
		session('username',null);
		session('roleinfo',null);*/
		session(null);
		setcookie(session_name(),session_id(),-1,'/');
		$this->success('退出成功',U('Login/index'));
	}


}